package com.fivefu.core.camera.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fivefu.base.common.utils.str.StrUtils;
import com.fivefu.base.web.vo.ResultInfo;
import com.fivefu.core.camera.services.CameraTreeService;
import com.fivefu.core.camera.services.SceneEventService;
import com.fivefu.core.camera.vo.request.DbSceneCameraVo;
import com.fivefu.core.camera.vo.request.DbSceneEventVo;
import com.fivefu.core.camera.vo.response.SceneCameraBean;
import com.fivefu.core.data.vo.TreeVo;
import com.fivefu.core.module.auth.utils.SecurityUtil;
import com.fivefu.core.skyeye.event.utils.VerifyDataUtils;
import com.fivefu.core.skyeye.event.vo.SceneVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.fivefu.base.web.controller.BaseController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 场景工单表 前端控制器
 * </p>
 *
 * @author 
 * @since 2022-09-19 19:38:06
 */
@Api(tags = "场景管理")
@RestController
@RequestMapping("/db-scene-event")
public class DbSceneEventController extends BaseController {

    private Logger logger = LoggerFactory.getLogger(DbSceneEventController.class);

    @Autowired
    private SceneEventService sceneEventService;
    @Autowired
    private CameraTreeService cameraTreeService;

    @ApiOperation(value = "查询识别场景详情",httpMethod = "POST",response = ResultInfo.class,notes = "查询识别场景详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "场景编号",paramType = "query",required = true, dataType = "Long"),
    })
    @RequestMapping(path = "/querySceneDetail",method = RequestMethod.POST)
    public ResultInfo querySceneDetail(Long id){
        try {
            if (id == null){
                return ResultInfo.renderError("场景编号不能为空");
            }
            Map<String, Object> sceneMap = sceneEventService.querySceneEvent(id);
            return ResultInfo.renderSuccess(sceneMap);
        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }
        return ResultInfo.renderError("查询识别场景详情失败");
    }

    @RequestMapping(path = "/editScene",method = RequestMethod.POST)
    @ApiOperation(value = "编辑场景",httpMethod = "POST",response = ResultInfo.class,notes = "编辑场景")
    public ResultInfo editScene(@ModelAttribute @Valid SceneVo sceneVo, DbSceneEventVo dbSceneEventVo, @RequestAttribute String userid){
        try {
            logger.info("编辑场景信息接收的参数："+JSON.toJSONString(request.getParameterMap()));
            String value = VerifyDataUtils.verifyRequiredColumn(sceneVo);
            if (StrUtils.isNotNull(value)){
                return ResultInfo.renderError(value + "不能为空");
            }
            //验证场景有效时间格式
            if (StrUtils.isNotNull(sceneVo.getSceneValidTime())){
                try {
                    Object timeArr[] = (Object[]) VerifyDataUtils.verifyJSONArray(sceneVo.getSceneValidTime());
                    for (int i = 0 ; i < timeArr.length ; i ++){
                        Object arr[] = (Object[]) timeArr[i];
                        for (int j = 0 ; j < arr.length ; j ++){
                            String time = (String) arr[j];
                            VerifyDataUtils.verifyDateTime(time, "HHmm");
                        }
                    }
                }catch (Exception e){
                    return ResultInfo.renderError("场景有效时间格式不正确");
                }
            }
            //验证场景特定识别参数格式
            if (StrUtils.isNotNull(sceneVo.getCvParam())){
                try {
                    VerifyDataUtils.verifyJSONArray(sceneVo.getCvParam());
                }catch (Exception e){
                    return ResultInfo.renderError("场景识别参数格式不正确");
                }
            }

            boolean flag = sceneEventService.editSceneEvent(sceneVo,dbSceneEventVo, userid);
            if(flag) return ResultInfo.renderSuccess("编辑场景成功");
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return   ResultInfo.renderError("编辑场景失败");
    }

    @RequestMapping(path = "/querySceneCameraByPage",method = RequestMethod.POST)
    @ApiOperation(value = "分页获取场景探头列表",httpMethod = "POST",response = ResultInfo.class,notes = "分页获取场景探头列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sceneId", value = "场景编号", paramType = "query",required = true, dataType = "Long"),
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query",required = true, dataType = "int"),
            @ApiImplicitParam(name = "limit", value = "数量", paramType = "query",required = true, dataType = "int")
    })
    public ResultInfo querySceneCameraByPage(Long sceneId,Integer page,Integer limit){
        try {
            logger.info("分页获取场景探头列表接收的参数："+ JSON.toJSONString(request.getParameterMap()));
            if (sceneId == null){
                return   ResultInfo.renderError("场景编号不能为空");
            }
            //判断当前页是否为空
            if (page==null){
                return   ResultInfo.renderError("当前页不能为空");
            }
            //判断每页显示条数是否为空
            if (limit==null){
                return   ResultInfo.renderError("每页显示条数不能为空");
            }
            Page<SceneCameraBean> list = sceneEventService.querySceneCamera(sceneId, page, limit);
            return ResultInfo.renderSuccess(0,"查询场景下探头列表成功",list.getTotal(),list.getRecords());
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return   ResultInfo.renderError("分页获取场景探头列表失败");
    }

    @RequestMapping(path = "/queryAddSceneCameraList",method = RequestMethod.POST)
    @ApiOperation(value = "查询增加探头列表",httpMethod = "POST",response = ResultInfo.class,notes = "查询增加探头列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sceneId", value = "场景编号", paramType = "query",required = true, dataType = "Long"),
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query",required = true, dataType = "int"),
            @ApiImplicitParam(name = "limit", value = "数量", paramType = "query",required = true, dataType = "int"),

            @ApiImplicitParam(name = "cameraName", value = "探头名称", paramType = "query",required = false, dataType = "String"),
            @ApiImplicitParam(name = "cameraCode", value = "探头编号", paramType = "query",required = false, dataType = "String"),
            @ApiImplicitParam(name = "labelName", value = "标签名称", paramType = "query",required = false, dataType = "String"),
            @ApiImplicitParam(name = "areaid", value = "所属区域", paramType = "query",required = false, dataType = "String"),
            @ApiImplicitParam(name = "streetid", value = "所属街道", paramType = "query",required = false, dataType = "String"),
            @ApiImplicitParam(name = "cameraManufacturer", value = "厂商", paramType = "query",required = false, dataType = "String"),
            @ApiImplicitParam(name = "cameraStatus", value = "探头状态", paramType = "query",required = false, dataType = "String"),
            @ApiImplicitParam(name = "onlineStatus", value = "在线状态", paramType = "query",required = false, dataType = "String"),
    })
    public ResultInfo queryAddSceneCameraList(Long sceneId,Integer page,Integer limit){
        try {
            logger.info("查询增加探头列表接收的参数："+ JSON.toJSONString(request.getParameterMap()));
            if (sceneId == null){
                return   ResultInfo.renderError("场景编号不能为空");
            }
            //判断当前页是否为空
            if (page==null){
                return   ResultInfo.renderError("当前页不能为空");
            }
            //判断每页显示条数是否为空
            if (limit==null){
                return   ResultInfo.renderError("每页显示条数不能为空");
            }
            String cameraName = StrUtils.isCheckNull(request.getParameter("cameraName"));
            String cameraCode = StrUtils.isCheckNull(request.getParameter("cameraCode"));
            String labelName = StrUtils.isCheckNull(request.getParameter("labelName"));
            String areaid = StrUtils.isCheckNull(request.getParameter("areaid"));
            String streetid = StrUtils.isCheckNull(request.getParameter("streetid"));
            String cameraManufacturer = StrUtils.isCheckNull(request.getParameter("cameraManufacturer"));
            String cameraStatus = StrUtils.isCheckNull(request.getParameter("cameraStatus"));
            String onlineStatus = StrUtils.isCheckNull(request.getParameter("onlineStatus"));

            Page<SceneCameraBean> list = sceneEventService.queryAddCamera(sceneId,cameraName,cameraCode,labelName,areaid,streetid,cameraManufacturer,  cameraStatus,  onlineStatus, page, limit);
            return ResultInfo.renderSuccess(0,"查询增加探头列表列表成功",list.getTotal(),list.getRecords());
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return   ResultInfo.renderError("查询增加探头列表失败");
    }

    @RequestMapping(path = "/removeSceneCamera",method = RequestMethod.POST)
    @ApiOperation(value = "批量移除场景下探头",httpMethod = "POST",response = ResultInfo.class,notes = "批量移除场景下探头")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sceneId", value = "场景编号", paramType = "query",required = true, dataType = "Long"),
            @ApiImplicitParam(name = "cameraCode", value = "探头标识,多个用,分隔", paramType = "query",required = true, dataType = "String"),
    })
    public ResultInfo removeSceneCamera(Long sceneId, String cameraCode, @RequestAttribute String userid){
        try {
            logger.info("批量移除场景下探头接收的参数："+ JSON.toJSONString(request.getParameterMap()));
            if (sceneId == null){
                return ResultInfo.renderError("场景编号不能为空");
            }
            if (StrUtils.isNull(cameraCode)){
                return ResultInfo.renderError("探头标识不能为空");
            }
            sceneEventService.removeSceneCamera(sceneId,cameraCode,userid);
            return ResultInfo.renderSuccess("批量移除场景下探头成功");
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return   ResultInfo.renderError("批量移除场景下探头失败");
    }

    @RequestMapping(path = "/addSceneCamera",method = RequestMethod.POST)
    @ApiOperation(value = "批量增加场景下探头",httpMethod = "POST",response = ResultInfo.class,notes = "批量增加场景下探头")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cameraCode", value = "探头标识,多个用,分隔", paramType = "query",required = true, dataType = "String")
    })
    public ResultInfo addSceneCamera(@ModelAttribute @Valid DbSceneCameraVo dbSceneCameraVo, @RequestAttribute String userid){
        try {
            logger.info("批量增加场景下探头接收的参数："+ JSON.toJSONString(request.getParameterMap()));

            String cameraCode = request.getParameter("cameraCode");
            if (StrUtils.isNull(cameraCode)){
                return ResultInfo.renderError("探头标识不能为空");
            }
            //验证探头有效时间格式
            if (StrUtils.isNotNull(dbSceneCameraVo.getCameraValidTime())){
                try {
                    Object timeArr[] = (Object[]) VerifyDataUtils.verifyJSONArray(dbSceneCameraVo.getCameraValidTime());
                    for (int i = 0 ; i < timeArr.length ; i ++){
                        Object arr[] = (Object[]) timeArr[i];
                        for (int j = 0 ; j < arr.length ; j ++){
                            String time = (String) arr[j];
                            VerifyDataUtils.verifyDateTime(time, "HHmm");
                        }
                    }
                }catch (Exception e){
                    return ResultInfo.renderError("探头有效时间格式不正确");
                }
            }
            boolean flag = sceneEventService.addSceneCamera(cameraCode, dbSceneCameraVo,userid);
            if (flag){
                return ResultInfo.renderSuccess("批量增加场景下探头成功");
            }

        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return   ResultInfo.renderError("批量增加场景下探头失败");
    }

    @ApiOperation(value = "查询场景探头树形结构",httpMethod = "POST",response = ResultInfo.class,notes = "查询场景探头树形结构")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type",value = "查询类型(list,tree)",paramType = "query",required = true, dataType = "String"),
            @ApiImplicitParam(name = "sceneId",value = "场景Id",paramType = "query",required = true, dataType = "Long")
    })
    @RequestMapping(path = "/querySceneCameraTreeList",method = RequestMethod.POST)
    public ResultInfo querySceneCameraTreeList(String type,Long sceneId){
        logger.info("查询场景探头树形结构接收的参数："+ JSON.toJSONString(request.getParameterMap()));
        try {
            if (StrUtils.isNull(type)){
                return ResultInfo.renderError("查询类型不能为空");
            }
            if (sceneId == null){
                return ResultInfo.renderError("场景编号不能为空");
            }
            List<TreeVo> treeList = cameraTreeService.getAddMonitorRegionTreeBySceneId(type, sceneId);
            return ResultInfo.renderSuccess(treeList);
        }catch (Exception e){
            logger.error(e.getMessage(), e);
        }
        return ResultInfo.renderError("查询场景探头树形结构失败");
    }

}
